package uscdb.service.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

import org.apache.log4j.Logger;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import uscdb.dao.ProjectDaoI;
import uscdb.model.Tproject;
import uscdb.pageModel.DataGrid;
import uscdb.pageModel.Json;
import uscdb.pageModel.PageHelper;
import uscdb.pageModel.Project;
import uscdb.service.ProjectServiceI;
import uscdb.util.StringUtil.StringUtil;

@Service
public class ProjectServiceImpl implements ProjectServiceI {
	/**
	 * Logger for this class
	 */
	private static final Logger logger = Logger.getLogger(ProjectServiceImpl.class);

	@Autowired
	private ProjectDaoI projectDao;

	@Override
	public void add(Project project) {
		project.setId(UUID.randomUUID().toString());
		Tproject tproject = new Tproject();
		BeanUtils.copyProperties(project, tproject);
		projectDao.save(tproject);
	}

	@Override
	public void edit(Project project) {
		Tproject tproject = projectDao.get(Tproject.class, project.getId());
		try {
			if (null != tproject) {
				tproject.setModifydatetime(new Date());
				BeanUtils.copyProperties(project, tproject, new String[] { "id", "modifydatetime" });
			}
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
		}

	}

	@Override
	public Json delete(String ids) {
		Json json = new Json();
		for (String id : ids.split(",")) {
			Tproject tproject = projectDao.get(Tproject.class, id);
			projectDao.delete(tproject);
		}
		json.setMsg("删除成功");
		json.setSuccess(true);
		return json;
	}

	@Override
	public DataGrid dataGrid(Project project, PageHelper ph) {
		DataGrid dg = new DataGrid();
		List<Project> projectList = new ArrayList<>();
		Map<String, Object> params = new HashMap<>();
		String hql = " from Tproject t ";
		List<Tproject> tprojectList = projectDao.find(hql + whereHql(project, params) + orderHql(ph), params,
				ph.getPage(), ph.getRows());
		for (Tproject tproject : tprojectList) {
			Project project2Page = new Project();
			BeanUtils.copyProperties(tproject, project2Page);
			projectList.add(project2Page);
		}
		dg.setRows(projectList);
		dg.setTotal(projectDao.count("select count(*) " + hql + whereHql(project, params), params));
		return dg;
	}

	/**
	 * 排序函数
	 * 
	 * @param ph
	 *            分页对象
	 * @return
	 */
	private String orderHql(PageHelper ph) {
		String orderString = "";
		if (ph.getSort() != null && ph.getOrder() != null) {
			orderString = " order by t." + ph.getSort() + " " + ph.getOrder();
		}
		return orderString;
	}

	private String whereHql(Project project, Map<String, Object> params) {
		String hql = "";
		if (null != project) {
			hql += " where 1=1 ";
		}
		if (!StringUtil.isEmpty(project.getProductName())) {
			hql += " and t.productName like :productName ";
			params.put("productName", "%%" + project.getProductName().trim().toUpperCase() + "%%");
		}
		if (!StringUtil.isEmpty(project.getTargetVersion())) {
			hql += " and t.targetVersion like :targetVersion ";
			params.put("targetVersion", "%%" + project.getTargetVersion().trim().toUpperCase() + "%%");
		}
		if (!StringUtil.isEmpty(project.getType())) {
			hql += " and t.type like :type ";
			params.put("type", "%%" + project.getType().trim().toUpperCase() + "%%");
		}
		if (!StringUtil.isEmpty(project.getProgress())) {
			hql += " and t.progress like :progress ";
			params.put("progress", "%%" + project.getProgress().trim() + "%%");
		}
		return hql;
	}

}
